using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Collections.Generic;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using IGlobalist.Cms.Common;
using IGlobalist.Cms.Model;
using IGlobalist.Cms.Bll;

namespace IGlobalist.Cms.Admin.Control
{
    public partial class ResourceFolderAddEdit : AdminBaseUserControl 
    {
        public event EventHandler EventSaveSuccess;
        
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                IList<ICmsClass> cList = ClassController.GetClassListByClassTypeID((short)CmsObjectType.ResourceFolder);
                if (cList != null)
                {
                    ddlClassName.DataSource = cList;
                    ddlClassName.DataBind();
                    NewResourceFolder();        
                }
            }     
        }

               
        public void DoNewResourceFolder(object sender, EventArgs e)
        {
            NewResourceFolder(); 
            
        }

        protected void NewResourceFolder()
        {
         try
            {
                lblStatus.Text = "Create Folder";
                ViewState["ResourceFolder"] = null;
                fsExt.Visible = false;   
                ddlClassName.Enabled = true;
                if (ddlClassName.SelectedIndex == -1)
                    lbError.Text = "Folder Class is null";
                else
                    lbError.Text = "";  
                tbName.Text = "";
                tbName.Focus();                
                tbDescription.Text = "";                
                tbName.ReadOnly = false;
                hdfResourceFolderID.Value = "0"; 
                ddlParent.DataBind();                
                ICmsClass clazz = ClassController.GetClass(Convert.ToInt64(ddlClassName.SelectedValue));
                ucCmsExtPropertiesForm.PropertyList = clazz.PropertyList;
                if (ucCmsExtPropertiesForm.PropertyList.Count > 3)
                    fsExt.Visible = true;
                else fsExt.Visible = false;    
            }
            catch(Exception ex)
            {
                Logger.Error("[[ResourceFolderAddEdit]]", ex);  
            }      
        }


        public void DoSaveResourceFolder(object sender, EventArgs e)
        {
            try
            {
                bool success = false;
                ResourceFolder resourceFolder = new ResourceFolder();                
                if (ViewState["ResourceFolder"] != null)
                {   
                    resourceFolder = (ResourceFolder)ViewState["ResourceFolder"];
                    resourceFolder.EditedBy = IGlobalistMembership.GetUser().ID;
                    hdfResourceFolderID.Value=resourceFolder.ID.ToString();
                }                
                resourceFolder.Author = IGlobalistMembership.GetUser().ID;
                resourceFolder.Class = ClassController.GetClass(Convert.ToInt64(ddlClassName.SelectedValue));
                resourceFolder.ClassID = Convert.ToInt64(ddlClassName.SelectedValue);
                resourceFolder.ClassTypeID = Convert.ToInt16(CmsObjectType.ResourceFolder);                
                resourceFolder.ExtPropertyList = ucCmsExtPropertiesForm.PropertyList;
                resourceFolder.Name = tbName.Text;
                resourceFolder.Type = ResourceType.Folder;
                resourceFolder.ParentID = Convert.ToInt64(ddlParent.SelectedValue);
                resourceFolder.Description = tbDescription.Text;


                if (ViewState["ResourceFolder"] != null && ddlClassName.SelectedIndex!=-1)
                {
                    ResourceFolderController.UpdateResourceFolder(resourceFolder);
                    success = true;

                }
                else
                {
                    if (IsExist(tbName.Text) && ddlClassName.SelectedIndex!=-1 )
                    {
                        ResourceFolderController.InsertResourceFolder(resourceFolder);
                        success = true;
                    }
                }


                if ((EventSaveSuccess != null) && (success == true))
                {
                    EventSaveSuccess(this, new EventArgs());
                    lbError.Text = "";
                }
                else
                {
                    lbError.Text = "    Error! name: "+ tbName.Text + " is exist" ;
                    tbName.Focus();
                }
                if (success) lblStatus.Text = "Insert or Update Success";
            }
            catch (Exception ex)
            {
                Logger.Error("[[ResourceFolderAddEdit]]", ex);
            }
        }

        
        public void DoCopyResourceFolder(object sender, EventArgs e)
        {
            tbName.Text = "";
            tbName.ReadOnly = false;
            ViewState["ResourceFolder"] = null;
        }

        public void DoEditResourceFolder(object sender, EventArgs e)
        {
            try
            {
                lblStatus.Text = "Edit Folder";
                tbName.ReadOnly = true;                
                ddlClassName.Enabled = false;   
                ResourceFolderTree resFolderID = (ResourceFolderTree)sender;
                IResourceFolder resourceFolder = ResourceFolderController.GetResourceFolder(Convert.ToInt64(resFolderID.SelectedValue));
                ViewState["ResourceFolder"] = resourceFolder;
                ddlClassName.SelectedValue = resourceFolder.ClassID.ToString();     
                tbName.Text = resourceFolder.Name;
                //lbError.Text = "";
                tbDescription.Text = resourceFolder.Description;
                hdfResourceFolderID.Value = resourceFolder.ID.ToString();    
                tbDescription.Focus();                
                ddlParent.DataBind();
                ddlParent.SelectedValue = resourceFolder.ParentID.ToString();
                ICmsClass clazz = ClassController.GetClass(Convert.ToInt64(ddlClassName.SelectedValue));
                ucCmsExtPropertiesForm.PropertyList = resourceFolder.ExtPropertyList;
                if (ucCmsExtPropertiesForm.PropertyList.Count > 3)
                    fsExt.Visible = true;
                else fsExt.Visible = false;                
            }
            catch (Exception ex)
            {
                Logger.Error("[[ResourceFolderAddEdit]]", ex);
            }
            
        } 

        public bool IsExist(string name)
        {
            IList<IResourceFolder> list = ResourceFolderController.GetResourceFolderList(Convert.ToInt64(ddlClassName.SelectedValue));   
            foreach (IResourceFolder r in list)
            {
                if (r.Name == name)
                    return false;
            }
            return true;           
        }


        public void DoHidden(object sender, EventArgs e)
        {
            this.Visible = false;
        }

        public void DoShow(object sender, EventArgs e)
        {
            this.Visible = true;         
        }

        protected void ddlClassName_SelectedIndexChanged(object sender, EventArgs e)
        {
            ICmsClass clazz = ClassController.GetClass(Convert.ToInt64(ddlClassName.SelectedValue));
            ucCmsExtPropertiesForm.PropertyList = clazz.PropertyList;
            if (ucCmsExtPropertiesForm.PropertyList.Count > 3)
                fsExt.Visible = true;
            else fsExt.Visible = false;
        }
    }
}